package org.crmsystem.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.model.*;
import org.crmsystem.service.IDeptService;
import org.crmsystem.service.IJobService;
import org.crmsystem.service.IStaffService;
import org.crmsystem.service.IUserService;
import org.crmsystem.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/11.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IStaffService iStaffService;
    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IJobService iJobService;
    private static Logger logger= LogManager.getLogger(UserController.class);
    @RequestMapping("/getAllUser")
    public String getAllUser(){
        return "userList";
    }
    @ResponseBody
    @RequestMapping(value = "/getLikeUser",method = RequestMethod.POST)
    public Result<?> getAllUserList( Integer currPage, Integer pageSize,String startDate,String endDate,String select,String condition) throws ParseException{
        User user=new User();
        if("userName".equals(select)){
            user.setName('%'+condition+'%');
        }else if("roleName".equals(select)){
            user.setRoleName('%'+condition+'%');
        }else if ("staffName".equals(select)){
            user.setStaffName('%'+condition+'%');
        }else if("jobName".equals(select)){
            user.setJobName('%'+condition+'%');
        }else if("deptName".equals(select)){
            user.setDeptName('%'+condition+'%');
        }else if("useDate".equals(select)){
            user.setUseState('%'+condition+'%');
        }
        Map map=new HashMap();
        map.put("user",user);
        map.put("currPage",currPage);
        map.put("pageSize",pageSize);
        String sDate;
        if (startDate==null||startDate==""){
            sDate="1900-01-01";
        }else {
            sDate=startDate;
        }
        map.put("startDate",sDate);
        String eDate;
        if(endDate==null||endDate==""){
            eDate=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
        }else {
            eDate=endDate;
        }
        map.put("endDate",eDate);
        Page page=iUserService.getUser(map);
        logger.info(page.getResult().size());
        Map map1=new HashMap();
        map1.put("pageSize",pageSize);
        map1.put("currPage",currPage);
        map1.put("result",page.getResult());
        map1.put("totalPage",page.getTotalPage());
        map1.put("totalNum",page.getTotalNum());
        Result<Map<String,Object>> rl=new Result<>();
        rl.setData(map1);
        return rl;
    }
    @ResponseBody
    @RequestMapping(value="/addUser",method = {RequestMethod.POST})
    public Result<?> addUser( String userName, String password,int staffId, int deptId,int jobId, String createDate) throws ParseException{
        User user=new User();
        Date date;
        if (createDate == null || createDate == "") {
            date=new Date();
        }else {
             date = DateUtil.formatDate(createDate, "yyyy-MM-dd");
        }
        user.setCreateDate(date);
        logger.info(password);
        user.setName(userName);
        user.setStaffId(staffId);
        user.setPassword(password);
        user.setDeptId(deptId);
        user.setJobId(jobId);
        user.setRoleId(0);
        int flag=iUserService.addUser(user);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @RequestMapping("/getInfo")
    public ModelAndView getAllStaff(){
        Staff staff=new Staff();
        Dept dept=new Dept();
        Job job=new Job();
        Map map=new HashMap();
        map.put("staff",staff);
        map.put("currPage",1);
        map.put("pageSize",100);
        Page page=iStaffService.getStaff(map);
        List<Object> staffList=page.getResult();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        Map map1=new HashMap();
        map1.put("staffList",staffList);
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("addUser",map1);
    }
    @ResponseBody
    @RequestMapping("/getUser")
    public ModelAndView getUser(int id,String userName){
        User user=new User();
        user.setId(id);
        user.setName(userName);
        Map map=new HashMap();
        map.put("user",user);
        map.put("currPage",1);
        map.put("pageSize",100);
        Page page=iUserService.getUser(map);
        Staff staff=new Staff();
        Dept dept=new Dept();
        Job job=new Job();
        Map map2=new HashMap();
        map2.put("staff",staff);
        map2.put("currPage",1);
        map2.put("pageSize",100);
        Page page1=iStaffService.getStaff(map2);
        List<Object> staffList=page1.getResult();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        List<Object> list=page.getResult();
        User user1=(User)list.get(0);
        Map map1=new HashMap();
        map1.put("user",user1);
        map1.put("staffList",staffList);
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("editUser",map1);
    }
    @RequestMapping("toEditUser")
    public ModelAndView toEditUser(){
        Staff staff=new Staff();
        Dept dept=new Dept();
        Job job=new Job();
        Map map=new HashMap();
        map.put("staff",staff);
        map.put("currPage",1);
        map.put("pageSize",100);
        Page page=iStaffService.getStaff(map);
        List<Object> staffList=page.getResult();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        Map map1=new HashMap();
        map1.put("staffList",staffList);
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("editUser",map1);
    }
    @ResponseBody
    @RequestMapping(value="/editUser",method = {RequestMethod.POST})
    public Result<?> editUser(int userId,String userName, String password,int staffId, int deptId,int jobId, String createDate) throws ParseException{
        User user=new User();
        logger.info(createDate);
        Date date;
        if(createDate==null||createDate==""){
            date=new Date();
        }else {
            date = DateUtil.formatDate(createDate, "yyyy-MM-dd");
        }
        user.setCreateDate(date);
        user.setId(userId);
        user.setName(userName);
        user.setStaffId(staffId);
        user.setPassword(password);
        user.setDeptId(deptId);
        user.setJobId(jobId);
        user.setRoleId(0);
        int flag=iUserService.updateUser(user);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @RequestMapping("/toEditPassword")
    public ModelAndView toEditPassword(int id){
        User user=new User();
        user.setId(id);
        Map map=new HashMap();
        map.put("user",user);
        map.put("currPage",1);
        map.put("pageSize",100);
        Page page=iUserService.getUser(map);
        List<Object> list=page.getResult();
        User user1=(User)list.get(0);
        Map map1=new HashMap();
        map1.put("user",user1);
        return new ModelAndView("editPassword",map1);
    }
    @ResponseBody
    @RequestMapping(value = "editPassword",method = RequestMethod.POST)
    public Result<?> editPassword(String userName,String oldPassword,String newPassword){
        User user=new User();
        user.setName(userName);
        user.setPassword(oldPassword);
        Map map=new HashMap();
        map.put("user",user);
        map.put("newPassword",newPassword);
        int flag=iUserService.updateUserPassword(map);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @RequestMapping("/userIntroduce")
    public ModelAndView userIntroduce(int id,String userName){
        User user=new User();
        user.setId(id);
        user.setName(userName);
        Map map=new HashMap();
        map.put("user",user);
        map.put("currPage",1);
        map.put("pageSize",100);
        Page page=iUserService.getUser(map);
        List<Object> list=page.getResult();
        User user1=(User)list.get(0);
        Map map1=new HashMap();
        map1.put("user",user1);
        return new ModelAndView("userIntroduce",map1);
    }
    @ResponseBody
    @RequestMapping(value = "/delUser",method = RequestMethod.POST)
    public Result<?> delUser(int id){
        int flag=iUserService.delUser(id);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @ResponseBody
    @RequestMapping(value ="/delAllUser",method = RequestMethod.POST)
    public Result<?> delAllUser(String userId){
        String user[]=userId.split(",");
        int flag=1;
        for(int i=0;i<user.length;i++){
            if (user[i]!=null){
                int id=Integer.parseInt(user[i]);
                int influence=iUserService.delUser(id);
                if (influence<1){
                    flag=0;
                }
            }
        }
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
}
